

Export_information proc far
    ;函数: 导出数据
    ;无参数，无返回值

    ; 创建文件，返回句柄
    mov ax,strings
    mov ds,ax
    lea dx,doucument_name
    mov ah,3ch
    mov cx,00h
    int 21h
    jc Export_information_error_tranfer;判断是否出错
    push ax
    ; 文件句柄 = ss:[bp-6]

    

    ; 写入统计数据:  ss:[bp] 到 ss:[bp+16]
    mov ax,ss
    mov ds,ax
    mov dx,bp
    mov ah,40h
    mov cl,18h
    mov bx,ss:[bp-6]      ;取出文件句柄
    
    int 21h
    jc Export_information_error_tranfer

    ; 写入空格
    mov_dssi_to_cache_area
    clear_area_ds _8
    mov ah,40h
    mov cl,18h
    mov dx,si
    mov bx,ss:[bp-6]      ;取出文件句柄

    int 21h

    jmp Write_data


Export_information_error_tranfer:   
    jmp Export_information_error




Write_data:
    ; 写入学生数据
    ; ds指向data段
    mov ax,data
    mov ds,ax
    ; 获取学生人数
    mov cx,ss:[bp+16h]
    cmp cx,0h
    jz Export_information_error   ; 无数据时输出error

    mov ax,0h

Export_information_loop:
    push cx
    push ax

    ; 获取数据偏移地址
    call far ptr Get_pointers 
    mov dx,ax

    mov ah,40h
    mov cl,30h            ;一个学生信息为48字节
    mov bx,ss:[bp-6]      ;取出文件句柄
    int 21h

    pop ax
    pop cx
    inc ax
    loop Export_information_loop

Export_information_success:
    ;移动文件指针
    xor cx,cx
    xor dx,dx
    mov bx,ss:[bp-6]   
    mov al,0    
    mov ah,42h
    int 21h
    jc Export_information_error
    dispmsg Export_success

    pop ax
    retf

Export_information_error:
    dispmsg Export_error

    mov ax,bp
    sub ax,4h
    mov sp,ax ;令 sp = bp - 4 恢复栈顶指针  
    pop ax
    retf

Export_information endp

;==================================================================


Import_information  proc far
    ;函数: 导入数据
    ;无参数，无返回值




    ;打开文件，返回句柄
    mov ax,strings
    mov ds,ax
    lea dx,doucument_name
    mov ah,3dh
    mov al,0h  ;只读
    mov cx,00h
    int 21h
    jc Import_information_error_tranfer;判断是否出错
    push ax
    ; 文件句柄 = ss:[bp-4]
    jmp Write_statistics

Import_information_error_tranfer:
    jmp Import_information_error




Write_statistics:
    ;读取统计信息

    mov ax,ss
    mov ds,ax
    mov dx,bp

    mov ah,3fh
    mov bx,ss:[bp-6]
    mov cx,18h;读前24个 (统计信息)

    int 21h
    jc Import_information_error
    

    
;移动文件指针
    mov dx,30h
    xor cx,cx
    mov bx,ss:[bp-6]
    mov al,0    
    mov ah,42h
    int 21h
    jc Import_information_error


    mov cx,ss:[bp+16h]

    ;读取学生数据
    mov ax,data
    mov ds,ax


    lea dx,data_start

Write_data_loop:
    push cx
    mov ah,3fh
    mov bx,ss:[bp-6]
    mov cx,30h
    int 21h

    pop cx
    add dx,30h
    loop Write_data_loop









    ;重新设置指针

    ; 获取学生人数
    mov cx,ss:[bp+16h]

    mov ax,pointers
    mov ds,ax
    lea si,pointers_start
    mov ax,0h
Write_pointers_loop:
    mov ds:[si],ax
    add ax,30h
    add si,2h
    loop Write_pointers_loop

    dispmsg Import_success

    pop ax
    retf




Import_information_error:
    dispmsg Import_error

    mov ax,bp
    sub ax,4h
    mov sp,ax ;令 sp = bp - 4 恢复栈顶指针  
    pop ax
    retf



Import_information endp